Kinetis SDK Demo Applications User Guide  1.0.0-beta
Freescale Semiconductor, Inc.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
low_power_demo.h File Reference
#include <stdint.h>
#include "uart/fsl_uart_driver.h"
#include "smc/fsl_smc_manager.h"
#include "fsl_mcg_hal.h"

Go to the source code of this file.

Macros

#define LOW_POWER
 
#define HIGH_GAIN
 
#define CLK0_TYPE
 
#define MCGOUT
 
#define SWD_CLK_DISABLE
 
#define SWD_DIO_DISABLE
 
#define SWD_CLK_ENABLE
 
#define SWD_DIO_ENABLE
 
#define LED0_EN
 
#define LED1_EN
 
#define LED2_EN
 
#define LED3_EN
 
#define LED0_OFF
 
#define LED1_OFF
 
#define LED2_OFF
 
#define LED3_OFF
 
#define LED0_ON
 
#define LED1_ON
 
#define LED2_ON
 
#define LED3_ON
 
#define BLPI
 
#define FBI
 
#define FEI
 
#define FEE
 
#define FBE
 
#define BLPE
 
#define PBE
 
#define PEE
 
#define UNDEF_VALUE
 
#define ON
 
#define OFF
 
#define LLWU_PIN_DIS
 
#define LLWU_PIN_RISING
 
#define LLWU_PIN_FALLING
 
#define LLWU_PIN_ANY
 
#define LPTMR_USE_IRCLK
 
#define LPTMR_USE_LPOCLK
 
#define LPTMR_USE_ERCLK32
 
#define LPTMR_USE_OSCERCLK
 
#define CRYSTAL
 
#define CANNED_OSC
 
#define SLOW_IRC
 
#define FAST_IRC
 
#define MCG_OUT_FREQ
 
#define PLL0_PRDIV
 
#define PLL0_VDIV
 

Functions

void clock_monitor (uint8_t enable)
 
void init_hardware (void)
 Initialize Clock Manager for PORT, ADC, and LPTIMER. More...
 
void low_power_modes_test (void)
 main low power mode test routine More...
 
void llwu_init (void)
 initializes LLWU module More...
 
void led_init (void)
 configure LED pins for the tower/freedom hardware More...
 
int printf_demo (const char *format,...)
 
void demo_lptmr_isr (void)
 ISR Routine for Low Power Timer. More...
 
void port_isr (void)
 Port ISR routine for the on board switch. More...
 
void port_init (void)
 configure tower/freedom hardware port pins for the application More...
 
void set_for_lp (uint8_t outOfLp)
 Settings done before swithing in low power mode. More...
 
int32_t pee_to_blpe (void)
 switch from PEE mode to BLPE mode More...
 
int32_t blpe_to_pee (void)
 switch from BLPE mode to PEE mode More...
 
void jtag_tdo_pullup_enable (void)
 
void vlp_clock_config (int8_t nextMode)
 clock configuration for very low power More...
 
int32_t fll_freq (int32_t fllRef)
 returns the Frequency Locked Loop frequency More...
 
uint8_t what_mcg_mode (void)
 returns the the current MCG mode More...
 
void llwu_configure (uint32_t pinEn, uint8_t riseFall, uint8_t moduleEn)
 Set up the LLWU for wakeup the MCU from LLS and VLLSx modes from the selected pin or module. More...
 
void lptmr_init (uint32_t count, uint32_t clockSource)
 Low Power Timer initialization routine. More...
 
void exit_vlpr (void)
 VLPR mode exit routine. Puts the processor into normal run mode from VLPR mode. You can transition from VLPR to normal run using this function. More...
 
void enter_stop (smc_stop_submode_t partialStopOpt)
 STOP mode entry routine. Puts the processor into normal stop mode. In this mode core, bus and peripheral clocks are disabled. More...
 
int32_t enter_vlpr (void)
 VLPR mode entry routine.Puts the processor into very low power run mode. In this mode all clocks are enabled, but the core clock limited. The flash clock is limited to 1MHz or less. More...
 
void enter_wait (power_modes_t pMode)
 WAIT mode entry routine. Puts the processor into wait mode. In this mode the core clock is disabled (no code executing), but bus clocks are enabled (peripheral modules are operational). More...
 
void enter_vlls0 (uint8_t PORPOValue)
 VLLS0 mode entry routine. Puts the processor into VLLS0 mode from normal run mode or VLPR. More...
 
void enter_vlls1 (void)
 VLLS1 mode entry routine. Puts the processor into VLLS1 mode from normal run mode or VLPR. More...
 
void enter_vlls2 (void)
 VLLS2 mode entry routine. Puts the processor into VLLS2 mode from normal run mode or VLPR. More...
 
void enter_vlls3 (void)
 enter in VLLS3 mode More...
 
void enter_vlps (void)
 enter in VLPS mode More...
 
void enter_lls (void)
 LLS mode entry routine. Puts the processor into LLS mode from normal run mode or VLPR. More...
 
uint8_t atc (uint8_t ircSelect, int32_t ircFreq, int32_t mcgOutFreq)
 configure for MCG auto trimming More...
 
int32_t fee_fei (int32_t slowIrcFreqInner)
 switches from More...
 
int32_t fei_fbe (int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal)
 switches from FEI to FBE mode More...
 
int32_t fbe_fei (int32_t slowIrcFreq)
 
int32_t fei_fbi (int32_t ircFreq, uint8_t ircSelect)
 switches from FEI to FBI mode More...
 
int32_t fbi_fei (int32_t slowIrcFreqInner)
 switches from FBI to FEI mode More...
 
int32_t fbe_pbe (int32_t crystalVal, int8_t prDivVal, int8_t vDivVal)
 switches from FBE to PBE mode This function transitions the MCG from FBE mode to PBE mode. This function presently only supports OSC0 and PLL0. Support for OSC1 and PLL1 will be added soon The function requires the desired OSC and PLL be passed in to it for compatibility with the future support of OSC/PLL selection More...
 
int32_t pbe_pee (int32_t crystalVal)
 switches from PBE to PEE mode More...
 
int32_t pee_pbe (int32_t crystalVal)
 switches from PEE to PBE mode More...
 
int32_t pbe_fbe (int32_t crystalVal)
 switches from PBE to FBE mode More...
 
int32_t fbe_fbi (int32_t ircFreq, uint8_t ircSelect)
 switches from FBE to PLPE mode More...
 
int32_t fbi_fbe (int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal)
 switches from FBI to FBE mode More...
 
int32_t fbi_fee (int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal)
 
int32_t fbe_fee (int32_t crystalVal)
 
int32_t fee_fbe (int32_t crystalVal)
 switches from FEE to FBE mode More...
 
int32_t pbe_blpe (int32_t crystalVal)
 switches from PBE to BLPE mode More...
 
int32_t blpe_pbe (int32_t crystalVal, int8_t prDivVal, int8_t vDivVal)
 switches from BLPE to PBE mode Since PBE mode can be enterred via FBE -> BLPE modes, it cannot be assumed that the PLL has been previously configured correctly. That is why this general purpose driver has the PLL settings as passed parameters. More...
 
int32_t blpe_fbe (int32_t crystalVal)
 switches from BLPE to FBE mode More...
 
int32_t fbi_blpi (int32_t ircFreq, uint8_t ircSelect)
 switches from FBI to BLPI mode This function transitions the MCG from FBI mode to BLPI mode. This is achieved by setting the MCG_C2[LP] bit. There is no status bit to check so 0 is always returned if the function was called with the MCG in FBI mode. More...
 
int32_t blpi_fbi (int32_t ircFreq, uint8_t ircSelect)
 switches from BLPI to FBI mode More...
 
int32_t fei_fee (int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal)
 switches from FEI to FEE mode More...
 
int32_t fee_fbi (int32_t ircFreq, uint8_t ircSelect)
 switches from FEE to FBI mode More...
 
int32_t fbe_blpe (int32_t crystalVal)
 switches from FBE to PLPE mode More...
 
int32_t pll_init (int32_t crystalVal, mcg_hgo_select_t hgoVal, mcg_eref_clock_select_t erefsVal, int8_t prDivVal, int8_t vDivVal, uint8_t mcgOutSelect)
 Option to move from FEI to PEE mode or to just initialize the PLL. More...
 
void out_srs (void)
 checks the value in the SRS registers and sends messages to the terminal announcing the status at the start of the code. More...
 
void get_uart_data (uart_state_t *uartState, uint8_t *rxBuffer, uint32_t requestedByteCount)
 

Macro Definition Documentation

#define BLPE

BLPE MCG mode

#define BLPI

BLPI MCG mode

#define CANNED_OSC

CANNED_OSC

#define CLK0_TYPE

Clock type indication

#define CRYSTAL

Crystal

#define FAST_IRC

Fast Internal Reference Clock

#define FBE

FBE MCG mode

#define FBI

FBI MCG mode

#define FEE

FEE MCG mode

#define FEI

FEI MCG mode

#define HIGH_GAIN

High power indication during PLL initialization

#define LED0_EN

Enable target LED0

#define LED0_OFF

Turn off target LED0

#define LED0_ON

Turn on target LED0

#define LED1_EN

Enable target LED1

#define LED1_OFF

Turn off target LED1

#define LED1_ON

Turn on target LED1

#define LED2_EN

Enable target LED2

#define LED2_OFF

Turn off target LED2

#define LED2_ON

Turn on target LED2

#define LED3_EN

Enable target LED3

#define LED3_OFF

Turn off target LED3

#define LED3_ON

Turn on target LED3

#define LLWU_PIN_ANY

LLWU wake up through external pin at both rising and falling edges

#define LLWU_PIN_DIS

LLWU wake up through external pin is disabled

#define LLWU_PIN_FALLING

LLWU wake up through external pin falling edge

#define LLWU_PIN_RISING

LLWU wake up through external pin rising edge

#define LOW_POWER

Low power indication during PLL initialization

#define LPTMR_USE_ERCLK32

LP Timer modules uses 32KHz External Reference Clock

#define LPTMR_USE_IRCLK

LP Timer modules uses Internal Reference Clock

#define LPTMR_USE_LPOCLK

LP Timer modules uses 1KHz clock

#define LPTMR_USE_OSCERCLK

LP Timer modules uses External Reference Clock

#define MCG_OUT_FREQ

Given MCG OUT Clock frequency

#define MCGOUT

MCG out indication during PLL initialization

#define OFF
#define ON
#define PBE

PBE MCG mode

#define PEE

PEE MCG mode

#define PLL0_PRDIV

divide reference by 4 = 2 MHz

#define PLL0_VDIV

multiply reference by 24 = 48 MHz

#define SLOW_IRC

Slow Internal Reference Clock

#define SWD_CLK_DISABLE

Disable SWD port clock

#define SWD_CLK_ENABLE

Enable SWD port clock

#define SWD_DIO_DISABLE

Disable SWD port DIO

#define SWD_DIO_ENABLE

Enable SWD port DIO

#define UNDEF_VALUE

Function Documentation

uint8_t atc ( uint8_t  ircSelect,
int32_t  ircFreq,
int32_t  mcgOutFreq 
)
Parameters
ircSelect- 0 if slow irc, 1 if fast irc
ircFreq- IRC frequency
mcgOutFreq- MCG output frequency
int32_t blpe_fbe ( int32_t  crystalVal)
Parameters
crystalVal- external clock frequency in Hz
int32_t blpe_pbe ( int32_t  crystalVal,
int8_t  prDivVal,
int8_t  vDivVal 
)
Parameters
crystalVal- external clock frequency in Hz
prDivVal- value to divide the external clock source by to create the desired PLL reference clock frequency
vDivVal- value to multiply the PLL reference clock frequency by Return value : MCGCLKOUT frequency (Hz) or error code
int32_t blpe_to_pee ( void  )
int32_t blpi_fbi ( int32_t  ircFreq,
uint8_t  ircSelect 
)

This function transitions the MCG from BLPI mode to FBI mode. This is achieved by clearing the MCG_C2[LP] bit. There is no status bit to check so 0 is always returned if the function was called with the MCG in BLPI mode.

Parameters
ircFreq- IRC clock frequency
ircSelect- 0 if slow irc, 1 if fast irc Return value : MCGOUT frequency or error code
void clock_monitor ( uint8_t  enable)
void demo_lptmr_isr ( void  )
void enter_lls ( void  )

Mode transitions: RUN -> LLS VLPR -> LLS

NOTE: LLS mode will always exit to RUN mode even if you were in VLPR mode before entering LLS.

Wakeup from LLS mode is controlled by the LLWU module. Most modules cannot issue a wakeup interrupt in LLS mode, so make sure to setup the desired wakeup sources in the LLWU before calling this function.

void enter_stop ( smc_stop_submode_t  partialStopOpt)

Mode transitions: RUN -> STOP

This function can be used to enter normal stop mode. If you are executing in normal run mode when calling this function and AVLP = 0, then you will enter normal stop mode. If AVLP = 1 with previous write to PMPROT then you will enter VLPS mode instead.

STOP mode is exited using any enabled interrupt or RESET, so no exit_stop routine is needed.

Parameters
partialStopOpt- Partial Stop Option: 0x00 = STOP - Normal Stop Mode 0x40 = PSTOP1 - Partial Stop with both system and bus clocks disabled 0x80 = PSTOP2 - Partial Stop with system clock disabled and bus clock enabled 0xC0 = Reserved
void enter_vlls0 ( uint8_t  PORPOValue)

Mode transitions:

 RUN -> VLLS0, VLPR -> VLLS0

NOTE: VLLSx modes will always exit to RUN mode even if you were in VLPR mode before entering VLLSx.

Wakeup from VLLSx mode is controlled by the LLWU module. Most modules cannot issue a wakeup interrupt in VLLSx mode, so make sure to setup the desired wakeup sources in the LLWU before calling this function.

Parameters
PORPOValue- POR detect circuit is enabled/disabled
  • 0 POR detect circuit is enabled in VLLS0
  • 1 POR detect circuit is disabled in VLLS0
void enter_vlls1 ( void  )

Mode transitions: RUN -> VLLS1 VLPR -> VLLS1

NOTE: VLLSx modes will always exit to RUN mode even if you were in VLPR mode before entering VLLSx.

Wakeup from VLLSx mode is controlled by the LLWU module. Most modules cannot issue a wakeup interrupt in VLLSx mode, so make sure to setup the desired wakeup sources in the LLWU before calling this function.

void enter_vlls2 ( void  )

Mode transitions: RUN -> VLLS2 VLPR -> VLLS2

NOTE: VLLSx modes will always exit to RUN mode even if you were in VLPR mode before entering VLLSx.

Wakeup from VLLSx mode is controlled by the LLWU module. Most modules cannot issue a wakeup interrupt in VLLSx mode, so make sure to setup the desired wakeup sources in the LLWU before calling this function.

void enter_vlls3 ( void  )
int32_t enter_vlpr ( void  )

Mode transitions: RUN -> VLPR

exit_vlpr() function can be used to switch from VLPR back to RUN.

while in VLPR,VLPW or VLPS the exit to VLPR is not possible

Returns
PMSTAT - value or error code
           PMSTAT =  000_0001 Current power mode is RUN
                  =  000_0100 Current power mode is VLPR

           ERROR Code =  0x14 - already in VLPR mode
                      =  0x24 - REGONS never clear indicating stop regulation
void enter_vlps ( void  )
void enter_wait ( power_modes_t  pMode)

Mode transitions: RUN -> WAIT VLPR -> VLPW

This function can be used to enter normal wait mode or VLPW mode. If you are executing in normal run mode when calling this function, then you will enter normal wait mode. If you are in VLPR mode when calling this function, then you will enter VLPW mode instead.

NOTE: Some modules include a programmable option to disable them in wait mode. If those modules are programmed to disable in wait mode, they will not be able to generate interrupts to wake up the core.

WAIT mode is exited using any enabled interrupt or RESET, so no exit_wait routine is needed.

Parameters
pMode- specify the specific wait modes
         = kPowerModeVlpw to enter in VLPW mode
         = kPowerModeWait to enter in WAIT mode
void exit_vlpr ( void  )

Mode transitions: VLPR -> RUN

int32_t fbe_blpe ( int32_t  crystalVal)
Parameters
crystalVal- external clock frequency in Hz
int32_t fbe_fbi ( int32_t  ircFreq,
uint8_t  ircSelect 
)
Parameters
ircFreq- IRC clock frequency
ircSelect- 0 if slow irc, 1 if fast irc
int32_t fbe_fee ( int32_t  crystalVal)
int32_t fbe_fei ( int32_t  slowIrcFreq)
int32_t fbe_pbe ( int32_t  crystalVal,
int8_t  prDivVal,
int8_t  vDivVal 
)
Parameters
crystalVal- external clock frequency in Hz
prDivVal- value to divide the external clock source by to create the desired PLL reference clock frequency
vDivVal- value to multiply the PLL reference clock frequency by Return value : MCGCLKOUT frequency (Hz) or error code
int32_t fbi_blpi ( int32_t  ircFreq,
uint8_t  ircSelect 
)
Parameters
ircFreq- IRC clock frequency
ircSelect- 0 if slow irc, 1 if fast irc Return value : MCGOUT frequency or error code
int32_t fbi_fbe ( int32_t  crystalVal,
mcg_hgo_select_t  hgoVal,
mcg_eref_clock_select_t  erefsVal 
)
Parameters
crystalVal- external clock frequency in Hz
hgoVal- selects whether low power or high gain mode is selected for the crystal oscillator. This has no meaning if an external clock is used.
erefsVal- selects external clock (=kMcgErefClockSelectExt) or crystal osc (=kMcgErefClockSelectOsc) Return value : MCGCLKOUT frequency (Hz) or error code
int32_t fbi_fee ( int32_t  crystalVal,
mcg_hgo_select_t  hgoVal,
mcg_eref_clock_select_t  erefsVal 
)
int32_t fbi_fei ( int32_t  slowIrcFreqInner)
Parameters
slowIrcFreqInner- slow IRD frequency
int32_t fee_fbe ( int32_t  crystalVal)
Parameters
crystalVal- external clock frequency in Hz
int32_t fee_fbi ( int32_t  ircFreq,
uint8_t  ircSelect 
)
Parameters
ircFreq- IRC clock frequency
ircSelect- 0 if slow irc, 1 if fast irc Return value : MCGOUT frequency or error code
int32_t fee_fei ( int32_t  slowIrcFreqInner)
Parameters
slowIrcFreqInner- slow IRC frequency
int32_t fei_fbe ( int32_t  crystalVal,
mcg_hgo_select_t  hgoVal,
mcg_eref_clock_select_t  erefsVal 
)

Mode transition: FEI to FBE mode

This function transitions the MCG from FEI mode to FBE mode. This is achieved by setting the MCG_C2[LP] bit. There is no status bit to check so 0 is always returned if the function was called with the MCG in FBI mode. The MCGCLKOUT frequency does not change

Parameters
crystalVal- external clock frequency in Hz
hgoVal- selects whether low power or high gain mode is selected for the crystal oscillator. This has no meaning if an external clock is used.
erefsVal- selects external clock (=kMcgErefClockSelectExt) or crystal osc (=kMcgErefClockSelectOsc) Return value : MCGCLKOUT frequency (Hz) or error code
int32_t fei_fbi ( int32_t  ircFreq,
uint8_t  ircSelect 
)
Parameters
ircFreq- IRC clock frequency
ircSelect- 0 if slow irc, 1 if fast irc
int32_t fei_fee ( int32_t  crystalVal,
mcg_hgo_select_t  hgoVal,
mcg_eref_clock_select_t  erefsVal 
)

This function transitions the MCG from FEI mode to FEE mode. This is achieved by setting the MCG_C2[LP] bit. There is no status bit to check so 0 is always returned if the function was called with the MCG in FBI mode. The MCGCLKOUT frequency does not change

Parameters
crystalVal- external clock frequency in Hz
hgoVal- selects whether low power or high gain mode is selected for the crystal oscillator. This has no meaning if an external clock is used.
erefsVal- selects external clock (=kMcgErefClockSelectExt) or crystal osc (=kMcgErefClockSelectOsc) Return value : MCGCLKOUT frequency (Hz) or error code
int32_t fll_freq ( int32_t  fllRef)
Parameters
fllRef- reference FLL frequency
void get_uart_data ( uart_state_t *  uartState,
uint8_t *  rxBuffer,
uint32_t  requestedByteCount 
)
void init_hardware ( void  )

Conversion is completed Flag

void jtag_tdo_pullup_enable ( void  )
void led_init ( void  )
void llwu_configure ( uint32_t  pinEn,
uint8_t  riseFall,
uint8_t  moduleEn 
)
Parameters
pin_en- unsigned integer, bit position indicates the pin is enabled. More than one bit can be set to enable more than one pin at a time.
rise_fall- 0x00 = External input disabled as wakeup 0x01 - External input enabled as rising edge detection 0x02 - External input enabled as falling edge detection 0x03 - External input enablge as any edge detection
module_en- unsigned char, bit position indicates the module is enabled. More than one bit can be set to enabled more than one module

for example: if bit 0 and 1 need to be enabled as rising edge detect call this routine with pin_en = 0x0003 and rise_fall = 0x02

Note: to set up one set of pins for rising and another for falling, 2 calls to this function are required, 1st for rising then the second for falling.

void llwu_init ( void  )
void low_power_modes_test ( void  )
void lptmr_init ( uint32_t  count,
uint32_t  clockSource 
)
Parameters
count- initialize timer for 'count' miliseconds
clockSource- clock source
void out_srs ( void  )
int32_t pbe_blpe ( int32_t  crystalVal)
Parameters
crystalVal- crystal value
int32_t pbe_fbe ( int32_t  crystalVal)
Parameters
crystalVal- crystal value
int32_t pbe_pee ( int32_t  crystalVal)
Parameters
crystalVal- crystal value
int32_t pee_pbe ( int32_t  crystalVal)
Parameters
crystalVal- crystal value
int32_t pee_to_blpe ( void  )
int32_t pll_init ( int32_t  crystalVal,
mcg_hgo_select_t  hgoVal,
mcg_eref_clock_select_t  erefsVal,
int8_t  prDivVal,
int8_t  vDivVal,
uint8_t  mcgOutSelect 
)
Parameters
crystalVal- external clock frequency in Hz either from a crystal or square wave clock source
hgoVal- selects whether low power or high gain mode is selected for the crystal oscillator. This has no meaning if an external clock is used.
erefsVal- selects external clock (=0) or crystal osc (=1)
prdivVal- value to divide the external clock source by to create the desired PLL reference clock frequency
vdivVal- value to multiply the PLL reference clock frequency by
mcgOutSelect- 0 if the PLL is just to be enabled, non-zero if the PLL is used to provide the MCGOUT clock for the system.

Return value : PLL frequency (Hz) or error code

void port_init ( void  )
void port_isr ( void  )
int printf_demo ( const char *  format,
  ... 
)
void set_for_lp ( uint8_t  outOfLp)
Parameters
outOfLp- indicate whether to do the setting for going in/coming out of low power mode
         = 0 for going into low power mode
         = 1 for exiting low power mode
void vlp_clock_config ( int8_t  nextMode)
Parameters
nextMode= BLPI or BLPE
uint8_t what_mcg_mode ( void  )